package com.shm.leetcode.concurrent;

import java.util.concurrent.Semaphore;
/**
 * @author: shm
 * @dateTime: 2020/10/18 21:06
 * @description: h每获取一次释放一个o许可，o每次获取两个许可（即2次h后执行一次o）
 */
class H2O {

    public H2O() {
        
    }
    private Semaphore oxygen = new Semaphore(2);
    private Semaphore hydrogen = new Semaphore(2);
    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
		hydrogen.acquire();
        // releaseHydrogen.run() outputs "H". Do not change or remove this line.
        releaseHydrogen.run();
        oxygen.release();
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        oxygen.acquire(2);
        // releaseOxygen.run() outputs "O". Do not change or remove this line.
		releaseOxygen.run();
		hydrogen.release(2);
    }
}